<?php

namespace App\Console\Commands\Wanggui;

use App\Console\Commands\Poi\BusinessPoi;
use App\Models\Common\MapNationalBrandShopModel;
use App\Service\Common\AreaService;
use Illuminate\Console\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class RefreshBrandShopPcd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:refresh-brand-shop';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新品牌门店的省市区';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $consoleOutput = new ConsoleOutput();
        // 创建一个具有175072个任务的进度条
        $progressBar = new ProgressBar($consoleOutput, 175072);

        // 启动进度条
        $progressBar->start();

        MapNationalBrandShopModel::query()
            ->chunkById(50, function ($brandShops) use ($progressBar){
                foreach ($brandShops as $brandShop) {
                    $progressBar->advance();
                    $regionInfo              = AreaService::getAdcodeByProvinceCityDistrict($brandShop->province, $brandShop->city, $brandShop->district);
                    $brandShop->province     = $regionInfo['province']['name'];
                    $brandShop->provinceCode = $regionInfo['province']['code'];
                    $brandShop->city         = $regionInfo['city']['name'];
                    $brandShop->cityCode     = $regionInfo['city']['code'];
                    $brandShop->district     = $regionInfo['district']['name'];
                    $brandShop->districtCode = $regionInfo['district']['code'];
                    $brandShop->save();
                }
            });

        // 完成进度条
        $progressBar->finish();

        // 输出换行符，让结果更美观
        $consoleOutput->writeln('');

        // 修复一些数据为空的
        MapNationalBrandShopModel::query()
            ->where('provinceCode', 0)
            ->chunkById(50, function ($brandShops) {
                foreach ($brandShops as $brandShop) {
                    $administration = BusinessPoi::getAdministrationByCode($brandShop->lon, $brandShop->lat);

                    $regionInfo     = AreaService::getAdcodeByProvinceCityDistrict($administration['province'], $administration['city'], $administration['district']);

                    $this->info($regionInfo['province']['name'] . '-' . $regionInfo['city']['name'] . '-' . $regionInfo['district']['name']);

                    $brandShop->province     = $regionInfo['province']['name'];
                    $brandShop->city         = $regionInfo['city']['name'];
                    $brandShop->district     = $regionInfo['district']['name'];
                    $brandShop->provinceCode = $regionInfo['province']['code'];
                    $brandShop->cityCode     = $regionInfo['city']['code'];
                    $brandShop->districtCode = $regionInfo['district']['code'];
                    $brandShop->save();
                }
            });
    }
}
